package com.framewiki.gpt.web.controller;

import com.cdkjframework.core.controller.WebUiController;
import com.cdkjframework.util.tool.CopyUtils;
import com.framewiki.gpt.dto.request.CreateChatCompletionDto;
import com.framewiki.gpt.dto.request.CreateChatCompletionVo;
import com.framewiki.gpt.dto.response.ChatCompletionResponseDto;
import com.framewiki.gpt.dto.response.ChatCompletionResponseVo;
import com.framewiki.gpt.service.TokenService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ProjectName: framewiki-gpt
 * @Package: com.framewiki.gpt.web.controller
 * @ClassName: TokenController
 * @Description: java类作用描述
 * @Author: xiaLin
 * @Date: 2023/6/21 23:38
 * @Version: 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/authorized")
public class TokenController extends WebUiController {

  /**
   * 受权
   */
  private final String TOKEN = "token";

  /**
   * 受权token
   */
  private final TokenService tokenServiceImpl;

  /**
   * 获取受权
   *
   * @param content 请求内容
   * @return 返回（token）结果
   */
  @PostMapping(value = "/token")
  public void getToken(@RequestBody @Validated CreateChatCompletionVo content) {
    String token = getRequestHeader(TOKEN);
    // 数据转换
    CreateChatCompletionDto completionDto = CopyUtils.copyNoNullProperties(content, CreateChatCompletionDto.class);
    // 返回受权
    String tokenValue = tokenServiceImpl.getToken(completionDto, token);
    write(tokenValue);
  }
}
